#!/bin/ksh
set -x
#
# Usage : gendtActions [COSE_LOCALE_NAME] [COMMAND]
#
#         Add an action with COMMAND/SYS_LOCALE_NAME to $HOME/.dt/types/$COSE_LOCALE_NAME.dt
#

#exec 2> /dev/null

#
# Generate ..
#   ACTIONS $ACTION_NAME
#

COSE_LOC_NAME=$1
NAME_PATTERN=$2

#
# The following are the lists of menu items per each flag (pattern)
#   It is a list of "Menu_Text Cmd_To_Exec".  We can extend as we
#   get better.
#
COMMAND_NAMES="Terminal langterm\n\
    Msmit langsmit\n\
    XmEditor langxmedit\n\
    Showkeyboard langxmkb\n\
    SetKeyboard setkb"


if [ ! -d $HOME/.dt ]
then
    mkdir $HOME/.dt
fi
if [ ! -d $HOME/.dt/types ]
then
    mkdir $HOME/.dt/types
fi

# Compose the name for ACTION, RULE_FILE
RULE_FILE=$HOME/.dt/types/$COSE_LOC_NAME".dt"

ALL_ACTIONS=""
echo $COMMAND_NAMES |
while read MENU_NAME COMMAND
do

    #
    # For each locale associated with the NAME_PATTERN
    #   generate a different action.
    #
    ACTIONS=""
    ACTION_NAME=""
    grep $NAME_PATTERN $HOME/tables/ASCII | 
    awk -F: '{print $1" " $3" "$6 }' | 
    while read COSE_LOC_NAME SYS_LOC_NAME CS_CLASS
    do
    ACTION_NAME="($CS_CLASS)$MENU_NAME"
    if [ "$ACTIONS" = "" ]
    then
       ACTIONS=$ACTION_NAME
    else
       ACTIONS="$ACTIONS,$ACTION_NAME"
    fi

    cat >> $RULE_FILE << EOF_ACTIONS
ACTION $ACTION_NAME
{
    ARG_TYPE	    $1_ATTR
    TYPE            COMMAND
    WINDOW_TYPE     NO_STDIO
    EXEC_STRING     $COMMAND $COSE_LOC_NAME
    DESCRIPTION     $COMMAND for $COSE_LOC_NAME
}
        
EOF_ACTIONS

    ACTION_NAME=""
    done

if [ "$ALL_ACTIONS" = "" ]
then
   ALL_ACTIONS=$ACTIONS
else
   ALL_ACTIONS="$ALL_ACTIONS,$ACTIONS"
fi

done

#
# echo $ACTION_NAME to pass it to gendt
#
/bin/echo $ALL_ACTIONS
